﻿using System;
using System.Collections.Generic;
using System.Text;
using AspNetCs.IDAL;
using System.Data;
using AspNetCs.Model;
using AspNetCs.Common;

namespace AspNetCs.SQLServerDAL
{
    public class UserPhoto_class_DAL : IUserPhoto_Class
    {
        #region IUserPhoto_Class 成员

        public DataTable GetDataTable(string filter)
        {
            DataTable Dt = new DataTable();
            db Conn = new db();
            string sqlstr = "Select * from UserPhoto_class ";
            if (filter != "")
            {
                sqlstr += " where " + filter;
            }
            Dt = Conn.GetSql_DataTable(sqlstr);
            return Dt;
        }
        public int Update(UserPhoto_Class_Model md)
        {
            int result = 0;
            try
            {
                db Conn = new db();
                db.ParameterArry[] par = new db.ParameterArry[3];
                par[0].dbType = SqlDbType.VarChar;
                par[0].objValue = md.ClassName;
                par[0].parName = "@ClassName";

                par[1].dbType = SqlDbType.VarChar;
                par[1].objValue = md.Readme;
                par[1].parName = "@Readme";


                par[2].dbType = SqlDbType.Int;
                par[2].objValue = md.Id;
                par[2].parName = "@ID";

                string sqlstr = "Update UserPhoto_class set ClassName=@ClassName,Readme=@Readme  where id=@ID";

                db Cn = new db();
                result = Cn.ExcSql(sqlstr, par);
            }
            catch
            {
                result = -1;
            }
            return result;
        }

        public int Add(UserPhoto_Class_Model md)
        {
            int result = 0;
            db conn = new db();
            try
            {
                db.ParameterArry[] par = new db.ParameterArry[2];
                par[0].dbType = SqlDbType.VarChar;
                par[0].objValue = md.ClassName;
                par[0].parName = "@ClassName";

                par[1].dbType = SqlDbType.VarChar;
                par[1].objValue = md.Readme;
                par[1].parName = "@Readme";
                string Sqlstr = "Insert into UserPhoto_class(ClassName,Readme) values(@ClassName,@Readme)";
                result = conn.ExcSql(Sqlstr, par);
            }
            catch
            {
                result = -1;
            }
            finally
            {
                conn.close();
            }
            return result;
        }
        public int del(UserPhoto_Class_Model md)
        {
            int result = 0;
            db Conn = new db();
            try
            {
                string Sqlstr = " begin tran ";
                Sqlstr += "  delete From UserPhoto_class where id=" + md.Id;
                Sqlstr += "  delete From UserPhoto where classID=" + md.Id;
                Sqlstr += "  commit tran ";
                result = Conn.ExcSql(Sqlstr);
            }
            catch
            {
                result = -1;
            }
            Conn.close();
            return result;
        }

        public UserPhoto_Class_Model GetModel(DataRow dr)
        {
            UserPhoto_Class_Model model = new UserPhoto_Class_Model();
            if (dr["id"] != null) model.Id = StringHelper.isNumber_int(dr["id"].ToString());
            if (dr["ClassName"] != null) model.ClassName = dr["ClassName"].ToString();
            if (dr["Readme"] != null) model.Readme = dr["Readme"].ToString();
            if (dr["Addtime"] != null) model.Addtime = StringHelper.isDatetime(dr["Addtime"].ToString());
            return model;
        }

        public T GetModelColumn<T>(string columnName, int id)
        {
            T Result = default(T);
            db Conn = new db();
            DataTable dt = Conn.GetSql_DataTable("Select " + columnName + "  From UserPhoto_Class where id=" + id.ToString());
            foreach (DataRow dr in dt.Rows)
            {
                Result = (T)dr[0];
            }
            return Result;
        }

        #endregion
    }
}
